/**
 * 用水单元
 */
Ext.define('Admin.view.waterOptimum.resultshow.useWaterUnit',{
	extend:'Ext.panel.Panel',
	xtype:'woUseWaterUnit',
	//起别名
	alias:'woUseWaterUnit',
	require:[],
	user:[],
	userCLs:'big-100 small-100',
	layout: {
        type: 'vbox',
        align: 'stretch'
    },
	items: [{
		xtype: 'grid',
		store: {xtype:'store'},
		border: true,
		features: [{
	        ftype: 'summary'
	    }],
		columns: [
			{ text: '时间', dataIndex: 'index1', minWidth: 100, align: 'center', summaryType: 'count', summaryRenderer: function(value, summaryData, dataIndex) {
                return '<span style="font-weight:bold;">合计</span>';
            }},
			{text: '农业', columns:[
				{ text: '需水', dataIndex: 'index2', minWidth: 100, align: 'center', renderer:
						function customRound(value, record){
							if(value == null)return null;
							return Ext.util.Format.round(value, 3);
						}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
			                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
			            }
				},
				{ text: '供水', dataIndex: 'index3', minWidth: 100, align: 'center', renderer:
						function customRound(value, record){
							if(value == null)return null;
							return Ext.util.Format.round(value, 3);
						}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
			                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
			            }
				},
				{ text: '缺水', dataIndex: 'index4', minWidth: 100, align: 'center', renderer:
						function customRound(value, record){
							if(value == null)return null;
							return Ext.util.Format.round(value, 3);
						}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
			                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
			            }
				},
				{ text: '缺水率', dataIndex: 'index5', minWidth: 100, align: 'center', renderer:
						function customRound(value, record){
							if(value == null)return null;
							return Ext.util.Format.round(value, 3);
						}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
							var data = new Array();
							for(var key in summaryData){
								data.push(summaryData[key]);
							}
							console.log(data);
			                return '<span style="font-weight:bold;">'+(data[3]/data[1])+'</span>';
			            }
				}]},
    		{text: '工业', columns:[
    			{ text: '需水', dataIndex: 'index6', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
				                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
				            }
					},
    			{ text: '供水', dataIndex: 'index7', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
				                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
				            }
					},
    			{ text: '缺水', dataIndex: 'index8', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
				                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
				            }
					},
    			{ text: '缺水率', dataIndex: 'index9', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'count', summaryRenderer: function(value, summaryData, dataIndex) {
								var data = new Array();
								for(var key in summaryData){
									data.push(summaryData[key]);
								}
				                return '<span style="font-weight:bold;">'+(data[7]/data[5])+'</span>';
				            }
					},]},
    		{text: '生活', columns:[
    			{ text: '需水', dataIndex: 'index10', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
				                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
				            }
					},
    			{ text: '供水', dataIndex: 'index11', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
				                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
				            }
					},
    			{ text: '缺水', dataIndex: 'index12', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
				                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
				            }
					},
    			{ text: '缺水率', dataIndex: 'index13', minWidth: 100, align: 'center', renderer:
							function customRound(value, record){
								if(value == null)return null;
								return Ext.util.Format.round(value, 3);
							}, summaryType: 'count', summaryRenderer: function(value, summaryData, dataIndex) {
								var data = new Array();
								for(var key in summaryData){
									data.push(summaryData[key]);
								}
				                return '<span style="font-weight:bold;">'+(data[11]/data[9])+'</span>';
				            }
					}]},
    		{text: '生态', hidden: true, columns:[
        		{ text: '需水', dataIndex: 'index14', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}
						},
        		{ text: '供水', dataIndex: 'index15', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}
						},
        		{ text: '缺水', dataIndex: 'index16', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}
						},
        		{ text: '缺水率', dataIndex: 'index17', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}
						}]},
        	{text: '合计', columns:[
        		{ text: '需水', dataIndex: 'index18', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
					                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
					            }
						},
        		{ text: '供水', dataIndex: 'index19', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
					                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
					            }
						},
        		{ text: '缺水', dataIndex: 'index20', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}, summaryType: 'sum', summaryRenderer: function(value, summaryData, dataIndex) {
					                return '<span style="font-weight:bold;">'+Ext.util.Format.round(value, 3)+'</span>';
					            }
						},
        		{ text: '缺水率', dataIndex: 'index21', minWidth: 100, align: 'center', renderer:
								function customRound(value, record){
									if(value == null)return null;
									return Ext.util.Format.round(value, 3);
								}, summaryType: 'count', summaryRenderer: function(value, summaryData, dataIndex) {
									var data = new Array();
									for(var key in summaryData){
										data.push(summaryData[key]);
									}
					                return '<span style="font-weight:bold;">'+(data[19]/data[17])+'</span>';
					            }
						}]}
		],
		flex: 1
	},{
		xtype: 'panel',
		layout: {
	        type: 'hbox',
	        align: 'stretch'
	    },
	    margin: '10 0 0 0',
		flex: 1,
		items: [{
			xtype: 'wohighchart',
			border: false,
			flex:1
		},{
			xtype: 'wohighchart',
			border: false,
			flex:1
		},{
			xtype: 'wohighchart',
			border: false,
			flex:1
		},{
			xtype: 'wohighchart',
			border: false,
			hidden: true,
			flex:1
		},{
			xtype: 'wohighchart',
			border: false,
			flex:1
		}]
	}],
	getChartConfig1: function(){
		return {
			title: {
		        text: null
		    },
		    xAxis: {
		        categories: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
		    },
		    yAxis: {
		        title: {
		            text: '水量 (亿m³)'
		        }
		    },
		    plotOptions: {
		    	spline: {
		    		marker: {
		    			enabled: false
		    		}
		    	}
		    },
		    series: [{
		        type: 'column',
		        name: '供水',
		        data: [0,0,0,0,0,0,0,0,0,0,0,0]
		    }, {
		        type: 'spline',
		        name: '需水',
		        data: [0,0,0,0,0,0,0,0,0,0,0,0],
		        marker: {
		            lineWidth: 2,
		            lineColor: Highcharts.getOptions().colors[3],
		            fillColor: 'white'
		        }
		    }]
		};
	},
	getChartConfig2: function(){
		return {
			title: {
		        text: null
		    },
		    xAxis: {
		        categories: ['农业','工业','生活','生态']
		    },
		    yAxis: {
		        title: {
		            text: '水量 (亿m³)'
		        }
		    },
		    series: [{
		        type: 'column',
		        name: '需水',
		        data: [0,0,0,0]
		    }, {
		    	type: 'column',
		        name: '供水',
		        data: [0,0,0,0]
		    }]
		};
	},
	dataReady: false,
	getStore: function(){
		return this.query('grid')[0].getStore();
	},
	getChart:function(i){
		return this.query('wohighchart')[i];
	},
	getDataArray: function(store, index) {
		var output = new Array();
		var t = 0;
		for (var i = 0; i < store.getCount(); i++) {
			output[t] = store.getAt(i).get(index);
			t++;
		}
		return output;
	},
	woSumArray: function(arr){
		var output = 0;
		for(var i=0;i<arr.length;i++){
			output += arr[i];
		}
		return output;
	},
	showCharts: function(){
		var panel = this;
		var obj = this.getStore();
		var timePacks = panel.getDataArray(obj, 'index1');
		if(this.dataReady){
			var configs = panel.getChartConfig1();
			configs.title.text = "农业";
			configs.xAxis.categories = timePacks;
			var series = configs.series;
			series[0].data = panel.getDataArray(obj, 'index3');
			series[1].data = panel.getDataArray(obj, 'index2');
			panel.getChart(0).chartDraw(configs);
		}
		if(this.dataReady){
			var configs = panel.getChartConfig1();
			configs.title.text = "工业";
			configs.xAxis.categories = timePacks;
			var series = configs.series;
			series[0].data = panel.getDataArray(obj, 'index7');
			series[1].data = panel.getDataArray(obj, 'index6');
			panel.getChart(1).chartDraw(configs);
		}
		if(this.dataReady){
			var configs = panel.getChartConfig1();
			configs.title.text = "生活";
			configs.xAxis.categories = timePacks;
			var series = configs.series;
			series[0].data = panel.getDataArray(obj, 'index11');
			series[1].data = panel.getDataArray(obj, 'index10');
			panel.getChart(2).chartDraw(configs);
		}
		if(this.dataReady){
			var configs = panel.getChartConfig1();
			configs.title.text = "生态";
			configs.xAxis.categories = timePacks;
			var series = configs.series;
			
			series[0].data = panel.getDataArray(obj, 'index15');
			series[1].data = panel.getDataArray(obj, 'index14');
			panel.getChart(3).chartDraw(configs);
		}
		if(this.dataReady){
			var configs = panel.getChartConfig2();
			configs.title.text = "汇总";
			var series = configs.series;
			series[0].data = [panel.woSumArray(panel.getDataArray(obj, 'index3')),
				panel.woSumArray(panel.getDataArray(obj, 'index7')),
				panel.woSumArray(panel.getDataArray(obj, 'index11')),
				panel.woSumArray(panel.getDataArray(obj, 'index15'))];
			series[1].data = [panel.woSumArray(panel.getDataArray(obj, 'index2')),
				panel.woSumArray(panel.getDataArray(obj, 'index6')),
				panel.woSumArray(panel.getDataArray(obj, 'index10')),
				panel.woSumArray(panel.getDataArray(obj, 'index14'))];
			series[0].data.pop();
			series[1].data.pop();
			panel.getChart(4).chartDraw(configs);
		}
	},
	displayData:function(data){
		this.getStore().setData(data);
		this.dataReady = true;
		this.showCharts();
	}
});
